package com.open.capacity.trade.service.impl;

import com.netflix.discovery.converters.Auto;
import com.open.capacity.common.exception.service.ServiceException;
import com.open.capacity.common.model.Concurrent;
import com.open.capacity.common.model.Records;
import com.open.capacity.common.model.Remittance;
import com.open.capacity.common.util.PageUtil;
import com.open.capacity.common.web.PageResult;
import com.open.capacity.common.web.Result;
import com.open.capacity.trade.dao.ConcurrentDao;
import com.open.capacity.trade.dao.RecordsDao;
import com.open.capacity.trade.dao.RemittanceDao;
import com.open.capacity.trade.feign.TradeFeignClient;
import com.open.capacity.trade.model.RemittanceExcel;
import com.open.capacity.trade.service.RecordsService;
import com.open.capacity.trade.service.RemittanceService;
import com.open.capacity.trade.vo.RemittanceVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@Service
public class RemittanceServiceImpl implements RemittanceService {
    @Autowired
    private RemittanceDao remittanceDao;
    @Autowired
    private RecordsService recordsService;
    @Autowired
    private ConcurrentDao concurrentDao;
    @Autowired
    private TradeFeignClient tradeFeignClient;
    @Transactional
    @Override
    public PageResult<RemittanceVo> findAllRemittance(Map<String, Object> params) throws ServiceException {
        try {
            //由于没有初始化数据，就先在页面展示数据时进行数据的导入
            //调用其它模块的feignClient实现远程调用
            //再将得到的数据进行导入表中
            //之后数据就是动态插入
//            Remittance remittance = new Remittance();
//            this.insertRemittance(remittance);
            int total = remittanceDao.count(params);
            List<RemittanceVo> list = Collections.emptyList();
            if (total > 0) {
                PageUtil.pageParamConver(params, true);
                list = remittanceDao.findAllRemittance(params);
            }
            return PageResult.<RemittanceVo>builder().data(list).code(0).count((long) total).build();
        } catch (Exception e) {
            throw new ServiceException(e);
        }
    }

    @Transactional
    @Override
    public void deleteRemittance(Long id,Long remittanceId) throws ServiceException {
        try{
            Remittance remittance=remittanceDao.findById(id,remittanceId);
            remittanceDao.deleteRemittance(id,remittanceId);
            log.info("删除汇款记录：{}",remittance);
        }catch (Exception e){
            throw new ServiceException(e);
        }
    }

    @Transactional
    @Override
    public Result saveOrUpdate(Remittance remittance) throws ServiceException {
        try{
            int result=0;
            remittance.setUpdateTime(new Date());
            Remittance employment = remittanceDao.findById(remittance.getId(),remittance.getRemittanceId());//查看当前汇款 是否已录用 是否付款
            if (remittance.getEmployment()==1){//变录用
                if (employment.getPayment()==0) {//变付款
                     result=remittanceDao.updateOrEnabled(remittance);
                     if (employment.getEmployment()==0){//0变1
                         Records records=new Records();
                         records.setCompanyName(remittance.getCompanyName());
                         records.setPosition(remittance.getJob());
                         records.setIndustry(remittance.getReasons());
                        result= recordsService.updateEntrys(records);//同步交易记录中的已录人数
                         if (result==0){
                             Concurrent concurrent=new Concurrent();
                             concurrent.setCompanyName(remittance.getCompanyName());
                             concurrent.setRecommendedName(remittance.getUsername());
                             concurrent.setJob(remittance.getJob());
                             concurrent.setEmployment(remittance.getEmployment());
                             concurrent.setUpdateTime(new Date());
                             result=concurrentDao.updateEmployment(concurrent);//同步兼职工资中的是否录用
                             return result > 0 ? Result.succeed( "操作成功") : Result.failed("操作失败");
                         }
                         return result < 0 ? Result.succeed( "操作成功") : Result.failed("操作失败");
                     }
                    return result > 0 ? Result.succeed( "操作成功") : Result.failed("操作失败");
                }else {//1
                    if (remittance.getPayment() == 1) {
                        result = remittanceDao.updateOrEnabled(remittance);
                        return result > 0 ? Result.succeed("操作成功") : Result.failed("操作失败");
                    }
                    return Result.failed("操作失败");
                }
            }else{//0
                return Result.failed("操作失败");
            }
        }catch (Exception e){
            throw new ServiceException(e);
        }
    }

    @Override
    public List<RemittanceExcel> findAllRemittanceExcel(Map<String, Object> params) throws ServiceException {
        try {
            List<RemittanceExcel> remittanceExcels = new ArrayList<>();
            List<RemittanceVo> list = remittanceDao.findAllRemittance(params);

            for (RemittanceVo remittance : list){
                RemittanceExcel remittanceExcel = new RemittanceExcel();
                BeanUtils.copyProperties(remittance,remittanceExcel);
                remittanceExcels.add(remittanceExcel);
            }
            return remittanceExcels;
        } catch (BeansException e) {
            throw new ServiceException(e);
        }
    }

    @Override
    public Result insertRemittance(Remittance remittance) throws ServiceException {
        try{
//            PageResult<Object> objectPageResult = tradeFeignClient.selectAllRemittanceDate();//先找出数据
            Long getTime;
            synchronized (remittance){
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
                String time=simpleDateFormat.format(new Date());
                String r=time.replace(" ","");
                String c=r.replaceAll(":","");
                getTime=Long.valueOf(String.valueOf(c));
            }
//            Remittance remittance = new Remittance();
            remittance.setRemittanceId(getTime);
//            remittanceDao.insertRemittance(remittance);//将数据放入汇款管理表
            remittance.setCreateTime(new Date());
            int result=0;
            result=remittanceDao.insertRemittance(remittance);
            return result > 0 ? Result.succeed("操作成功") : Result.failed("操作失败");
        }catch (Exception e){
            throw new ServiceException(e);
        }
    }

    @Override
    public Result updateRemittance(Remittance remittance) throws ServiceException {
        try{
            remittance.setUpdateTime(new Date());
            Integer result=remittanceDao.updateRemittance(remittance);
            return result > 0 ? Result.succeed("操作成功") : Result.failed("操作失败");
        }catch (Exception e){
            throw new ServiceException(e);
        }
    }


}
